package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderDerate;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.factory.TingshuFactory;
import com.atguigu.tingshu.order.listener.OrderTimeoutListener;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.order.service.OrderPayService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order/orderInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderInfoApiController {

	@Resource
	private TingshuFactory tingshuFactory;

	@Autowired
	private OrderPayService orderPayService;

	/**
	 * 订单确认接口 不写入数据库 展示数据给前端
	 * @param tradeVo
	 * @return
	 */
	@GuiguLogin
	@PostMapping("trade")
	public Result trade(@RequestBody TradeVo tradeVo){
		// 1001-专辑 1002-声音 1003-vip会员
		String itemType = tradeVo.getItemType();
		// 通过itemType这个值,能动态获取到实现类的bean
		OrderInfoService orderInfoService
						= tingshuFactory.getOrderInfoService(itemType);
		// 调用trade方法
		return Result.ok(orderInfoService.trade(tradeVo));
	}

	/**
	 * 提交订单: 写入数据库
	 * @param orderInfoVo
	 * @return
	 */
	@GuiguLogin
	@PostMapping(value = "/submitOrder")
	public Result submitOrder(@RequestBody OrderInfoVo orderInfoVo){
		//获取类型
		String itemType = orderInfoVo.getItemType();
		//通过itemType这个值,能动态获取到实现类的bean
		OrderInfoService orderInfoService =
						tingshuFactory.getOrderInfoService(itemType);
		//返回: sign 支付方式(最新)
		return Result.ok(orderInfoService.submitOrder(orderInfoVo));
	}

	@Autowired
	private OrderTimeoutListener orderTimeoutListener;

	/**
	 * 主动取消订单
	 * @param orderId
	 * @return
	 */
	@GuiguLogin
	@GetMapping(value = "/cancelOrder/{orderId}")
	public Result cancelOrder(@PathVariable(value = "orderId") Long orderId){
		orderTimeoutListener.cancelTimeoutOrder(orderId);
		return Result.ok();
	}

	/**
	 * 分页查询订单数据
	 * @param page
	 * @param size
	 * @return
	 */
	@GetMapping(value = "/findUserPage/{page}/{size}")
	@GuiguLogin
	public Result findUserPage(@PathVariable(value = "page") Integer page,
							   @PathVariable(value = "size") Integer size){
		//获取类型
		String itemType = SystemConstant.ORDER_ITEM_TYPE_ALL;
		//通过itemType这个值,能动态获取到实现类的bean
		OrderInfoService orderInfoService =
						tingshuFactory.getOrderInfoService(itemType);
		return Result.ok(orderInfoService.selectUserOrderPage(page, size));
	}

	/**
	 * 查询订单的详细信息
	 * @param orderNo
	 * @return
	 */
	@GetMapping(value = "/getOrderInfo/{orderNo}")
	@GuiguLogin
	public Result getOrderInfo(@PathVariable(value = "orderNo") String orderNo){
		//获取类型
		String itemType = SystemConstant.ORDER_ITEM_TYPE_ALL;
		//通过itemType这个值,能动态获取到实现类的bean
		OrderInfoService orderInfoService =
						tingshuFactory.getOrderInfoService(itemType);
		return Result.ok(orderInfoService.getOrderInfo(orderNo));
	}

	/**
	 * 申请订单的支付地址
	 * @param orderNo
	 * @return
	 */
	@GetMapping(value = "/getOrderPayAddress/{orderNo}")
	@GuiguLogin
	public Result getOrderPayAddress(@PathVariable(value = "orderNo") String orderNo){
		return Result.ok(orderPayService.getOrderPayAddress(orderNo));
	}

	/**
	 * 查询订单的支付结果:客服用-公司自己用!!!
	 * @param orderNo
	 * @return
	 */
	@GetMapping(value = "/getOrderPayResult/{orderNo}")
	@GuiguLogin
	public Result getOrderPayResult(@PathVariable(value = "orderNo") String orderNo){
		return Result.ok(orderPayService.getOrderPayResult(orderNo));
	}

}

